package com.chinaoly.sdk.exception;

import android.util.Log;

import com.google.gson.JsonParseException;

import org.apache.http.conn.ConnectTimeoutException;
import org.json.JSONException;

import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.text.ParseException;

/**
 * 异常处理
 */
public class ExceptionHandle {

    private static final String TAG = "ExceptionHandle";

    public static String handleException(Throwable e){
        String errorMsg;
        if (e instanceof SocketTimeoutException
                || e instanceof ConnectException
                || e instanceof ConnectTimeoutException
                || e instanceof UnknownHostException) {
            Log.e(TAG, "网络连接异常: " + e.getMessage());
            errorMsg = "网络连接异常";
        }
        else if (e instanceof JsonParseException
                || e instanceof JSONException
                || e instanceof ParseException) {   //均视为解析错误
            Log.e("TAG", "数据解析异常: " + e.getMessage());
            errorMsg = "数据解析异常";
        }
        else if (e instanceof ServerException) {//服务器返回的错误信息
            errorMsg = e.getMessage();
        }
        else if (e instanceof IllegalArgumentException) {
            errorMsg = "参数错误";
        }
        else {//未知错误
            try {
                Log.e("TAG", "错误: " + e.getMessage());
            } catch (Exception e1) {
                Log.e("TAG", "未知错误Debug调试 ");
            }
            errorMsg = "未知错误，可能抛锚了吧~";
        }
        return errorMsg;
    }
}
